package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.ITopicPlanService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.common.utils.MyUtil;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 课题计划表 Controller
 *
 * @author wmm
 * @date 2020-11-11 10:45:55
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class TopicPlanController extends BaseController {

    private final ITopicPlanService topicPlanService;

    /**
    * 课题计划表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicPlan")
    public String topicPlanIndex(){
        return MyUtil.view("business/topic/topicPlan");
    }


    @GetMapping("biz/topicPlan/list")
    @ResponseBody
//    @RequiresPermissions("topicPlan:list")
    public ResponseVO topicPlanList(QueryRequest request, TopicPlan topicPlan) {
        PageInfo<TopicPlan> pageInfo =  this.topicPlanService.findTopicPlans(request, topicPlan);
        return ResponseVO.success(pageInfo);
    }

    /**
     * 课题计划表列表页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/topicPlan/add")
    public String topicPlanAdd(){
        return MyUtil.view("business/topic/topicPlanAdd");
    }


    @ControllerEndpoint(operation = "新增TopicPlan", exceptionMessage = "新增TopicPlan失败")
    @PostMapping("biz/topicPlan")
    @ResponseBody
    @RequiresPermissions("topic:add")
    public ResponseVO addTopicPlan(@Valid TopicPlan topicPlan) {

        topicPlan.setIsGenerateCode(false);
        if (StringUtils.isEmpty(topicPlan.getYear())) {
            throw new MyException("请输入计划创建的年份！");
        }
        TopicPlan topicPlan1 = topicPlanService.selectAnnualByYear(topicPlan.getYear());
        if (topicPlan1 != null) {
            throw new MyException("对不起，当前年份的课题计划已创建！");
        }
        topicPlan.setStatusCode(DictEnum.PLAN_CREATE.getCode());
        //设置计划为年度课题
        topicPlan.setTopicClassifyCode(DictEnum.ANNUAL.getCode());
        topicPlan.setPeriod(topicPlan.getYear());
        topicPlanService.save(topicPlan);
        return ResponseVO.success();
    }




    @ControllerEndpoint(operation = "多个删除TopicPlan", exceptionMessage = "删除TopicPlan失败")
    @PostMapping("biz/topicPlan/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("topicPlan:delete")
    public ResponseVO deleteTopicPlans(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.topicPlanService.deleteTopicPlans(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个TopicPlan", exceptionMessage = "删除TopicPlan失败")
    @PostMapping("biz/topicPlan/delete/{topicPlanId}")
    @ResponseBody
    @RequiresPermissions("topicPlan:delete")
    public ResponseVO deleteTopicPlan(@PathVariable Long topicPlanId) {
        this.topicPlanService.removeById(topicPlanId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改TopicPlan", exceptionMessage = "修改TopicPlan失败")
    @PostMapping("biz/topicPlan/update")
    @ResponseBody
    @RequiresPermissions("topicPlan:update")
    public ResponseVO updateTopicPlan(TopicPlan topicPlan) {
        this.topicPlanService.updateTopicPlan(topicPlan);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改TopicPlan", exceptionMessage = "导出Excel失败")
    @PostMapping("biz/topicPlan/excel")
    @ResponseBody
    @RequiresPermissions("topicPlan:export")
    public void export(QueryRequest queryRequest, TopicPlan topicPlan, HttpServletResponse response) {
        List<TopicPlan> topicPlans = this.topicPlanService.findTopicPlans(queryRequest, topicPlan).getList();
        ExcelKit.$Export(TopicPlan.class, response).downXlsx(topicPlans, false);
    }
}
